package com.shenfc.fxzj.solr;

import com.shenfc.fxzj.constants.CodeConstants;
import com.shenfc.fxzj.domain.Blog;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import com.shenfc.fxzj.util.DateUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 博客索引类
 *
 * @author Administrator
 */
public class BlogSolrIndex {

    private final static Logger log = LoggerFactory.getLogger(BlogSolrIndex.class);


    /**
     * 添加博客索引
     *
     * @param blog
     */
    public void addIndex(Blog blog) throws Exception {
//        HttpSolrClient client = new HttpSolrClient(CodeConstants.SOLR_SERVER_URL);
//        SolrInputDocument doc = new SolrInputDocument();
//        doc.addField("id", blog.getUuid());
//        doc.addField("bid", blog.getId());
//        doc.addField("blog_title", blog.getTitle());
//        doc.addField("blog_createTime", new Date());
//        doc.addField("blog_summary", blog.getContentNoTag());
//        client.add(doc);
//        client.commit();
//        client.close();
    }

    /**
     * 更新博客索引
     *
     * @param blog
     * @throws Exception
     */
    public void updateIndex(Blog blog) throws Exception {
//        HttpSolrClient client = new HttpSolrClient(CodeConstants.SOLR_SERVER_URL);
//        SolrInputDocument doc = new SolrInputDocument();
//        doc.addField("id", blog.getUuid());
//        doc.addField("bid", blog.getId());
//        doc.addField("blog_title", blog.getTitle());
//        doc.addField("blog_createTime", new Date());
//        doc.addField("blog_summary", blog.getContentNoTag());
//        client.add(doc);
//        client.commit();
//        client.close();
    }

    /**
     * 删除指定博客的索引
     *
     * @param blogId
     * @throws Exception
     */
    public void deleteIndex(String blogId) throws Exception {
//        HttpSolrClient client = new HttpSolrClient(CodeConstants.SOLR_SERVER_URL);
//        client.deleteById(blogId);
//        client.commit();
//        client.close();
    }

    /**
     * 查询博客信息
     *
     * @return
     * @throws Exception
     */
    public List<Blog> searchBlog(String keyword, ModelAndView mav, int page) throws Exception {
        return null;
//        try {
//            HttpSolrClient client = new HttpSolrClient(CodeConstants.SOLR_SERVER_URL);
//            //创建查询对象
//            SolrQuery query = new SolrQuery();
//            //q 查询字符串，如果查询所有*:*
//            query.set("q", keyword);
//            //fq 过滤条件，过滤是基于查询结果中的过滤
////		query.set("fq", "product_catalog_name:幽默杂货");
//            //sort 排序，请注意，如果一个字段没有被索引，那么它是无法排序的
////        query.set("sort", "product_price desc");
//            //start row 分页信息，与mysql的limit的两个参数一致效果
//            query.setStart((page - 1) * CodeConstants.PAGE_SIZE);
//            query.setRows(CodeConstants.PAGE_SIZE);
//            //fl 查询哪些结果出来，不写的话，就查询全部，所以我这里就不写了
////        query.set("fl", "");
//            //df 默认搜索的域
//            query.set("df", "blog_keywords");
//            //======高亮设置===
//            //开启高亮
//            query.setHighlight(true);
//            //高亮域
//            query.addHighlightField("blog_title");
//            query.addHighlightField("blog_summary");
//            //前缀
//            query.setHighlightSimplePre("<h3 style='color:red'>");
//            //后缀
//            query.setHighlightSimplePost("</h3>");
//            //执行搜索
//            QueryResponse queryResponse = client.query(query);
//            //搜索结果
//            SolrDocumentList results = queryResponse.getResults();
//            //查询出来的数量
//            long numFound = results.getNumFound();
//            mav.addObject("pages", numFound % CodeConstants.PAGE_SIZE == 0 ? numFound / CodeConstants.PAGE_SIZE : numFound / CodeConstants.PAGE_SIZE + 1);
//            //遍历搜索记录
//            //获取高亮信息
//            List<Blog> blogList = new LinkedList<Blog>();
//            Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
//            for (SolrDocument solrDocument : results) {
//                Blog blog = new Blog();
//                blog.setId(Long.valueOf(solrDocument.get("bid").toString()));
//                SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
//                Date date = sdf.parse(solrDocument.get("blog_createTime").toString());
////				String sDate = DateUtil.formatDate(new Date((long) (date.getTime() + 8 * 60 * 60 * 1000)), DateUtil.DATETIME_FORMAT);
//                String sDate = DateUtils.dateToString( DateUtils.DATETIME_FORMAT,date);
//                blog.setCreateTimeStr(sDate);
//                //输出高亮
//                Map<String, List<String>> map = highlighting.get(solrDocument.get("id"));
//                List<String> titleList = map.get("blog_title");
//                List<String> contentList = map.get("blog_summary");
//                if (titleList != null && titleList.size() > 0) {
//                    blog.setTitle(titleList.get(0));
////					System.out.println(titleList.get(0));
//                } else {
//                    blog.setTitle(solrDocument.get("blog_title").toString());
//                }
//                if (contentList != null && contentList.size() > 0) {
//                    blog.setSummary(contentList.get(0));
////					System.out.println(contentList.get(0));
//                } else {
//                    blog.setSummary(solrDocument.get("blog_summary").toString());
//                }
//                blogList.add(blog);
////				System.out.println("商品id:" + solrDocument.get("id"));
////				System.out.println("博客标题 :" + solrDocument.get("blog_title"));
////				System.out.println("博客时间:" + solrDocument.get("blog_createTime"));
////				System.out.println("博客内容:" + solrDocument.get("blog_content"));
//            }
//            client.close();
//            return blogList;
//        } catch (Exception e) {
//            log.error("solr搜索引擎查询失败", e);
//            return Collections.EMPTY_LIST;
//        }
    }
}
